package org.orbis.index;

import java.io.PrintWriter;
import org.orbis.structure.ContentInfo;
import org.orbis.tool.Header;

public class IndexForm{
	private ContentInfo contentInfo;
	private PrintWriter out;

	public IndexForm(ContentInfo contentInfo, PrintWriter writer){
		this.contentInfo = contentInfo;
		this.out = writer;
	}

	public void putForm(){
		out.println("<!DOCTYPE html>");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
		out.println("<title>ORBIS Search System: Index</title>");
		out.println("</head>");
		out.println("<body>");
		// Title bar
		out.println(Header.getTitleBar("ORBIS Search System: Index"));

		// Header
		out.println(Header.getHeader(1));
		
		out.println("Please input value:");
		// GET process may make URL overflows
		out.println("<form method=\"POST\" action=\"index\">");

		out.println("Content (Required):");
		out.println("<table>");
		out.println("<tbody>");
		for(int i = 0; i < contentInfo.contentsNum(); i++){
			out.println("<tr>");
			out.println("<td>");
			out.println(contentInfo.contentName(i));
			out.println("</td>");
			out.println("<td>");
			out.println("<textarea col=10 row=200 name=\"" + contentInfo.contentName(i) + "\" required>");
			out.println("</textarea>");
			out.println("</td>");
			out.println("</tr>");
		}
		out.println("</tbody>");
		out.println("</table>");

		if(contentInfo.appendixNum() >= 1){
			out.println("<hr>");

			out.println("Appendix (Required):");
			out.println("<table>");
			out.println("<tbody>");
			for(int i = 0; i < contentInfo.appendixNum(); i++){
				out.println("<tr>");
				out.println("<td>");
				out.println(contentInfo.appendixName(i));
				out.println("</td>");
				out.println("<td>");
				out.println("<textarea col=10 row=200 name=\"" + contentInfo.appendixName(i) + "\" required>");
				out.println("</textarea>");
				out.println("</td>");
				out.println("</tr>");
			}
			out.println("</tbody>");
			out.println("</table>");
		}

		if(contentInfo.binaryFlagNum() >= 1){
			out.println("<hr>");

			out.println("Binary Flag (Optional):");
			out.println("<table>");
			out.println("<tbody>");
			for(int i = 0; i < contentInfo.binaryFlagNum(); i++){
				out.println("<tr>");
				out.println("<td>");
				out.println(contentInfo.binaryFlagName(i));
				out.println("</td>");
				out.println("<td>");
				out.println("<input type=\"radio\" name=\"" + contentInfo.binaryFlagName(i)
					+ "\" value=\"true\"> True ");
				out.println("<input type=\"radio\" name=\"" + contentInfo.binaryFlagName(i)
					+ "\" value=\"false\"> False ");
				out.println("</td>");
				out.println("</tr>");
			}
			out.println("</tbody>");
			out.println("</table>");
		}

		if(contentInfo.integerFlagNum() >= 1){
			out.println("<hr>");

			out.println("Integer value Flag (Optional):");
			out.println("<table>");
			out.println("<tbody>");
			for(int i = 0; i < contentInfo.integerFlagNum(); i++){
				out.println("<tr>");
				out.println("<td>");
				out.println(contentInfo.integerFlagName(i));
				out.println("</td>");
				out.println("<td>");
				out.println("<input type=\"number\" min=\"" + Integer.MIN_VALUE + "\" max=\""
					+ Integer.MAX_VALUE + "\" name=\"" + contentInfo.integerFlagName(i) + "\">");
				out.println("</td>");
				out.println("</tr>");
			}
			out.println("</tbody>");
			out.println("</table>");
		}

		if(contentInfo.longFlagNum() >= 1){
			out.println("<hr>");

			out.println("Long value Flag (Optional):");
			out.println("<table>");
			out.println("<tbody>");
			for(int i = 0; i < contentInfo.longFlagNum(); i++){
				out.println("<tr>");
				out.println("<td>");
				out.println(contentInfo.longFlagName(i));
				out.println("</td>");
				out.println("<td>");
				out.println("<input type=\"number\" min=\"" + Long.MIN_VALUE + "\" max=\""
					+ Long.MAX_VALUE + "\" name=\"" + contentInfo.longFlagName(i) + "\">");
				out.println("</td>");
				out.println("</tr>");
			}
			out.println("</tbody>");
			out.println("</table>");
		}

		if(contentInfo.floatFlagNum() >= 1){
			out.println("<hr>");

			out.println("Float value Flag (Optional):");
			out.println("<table>");
			out.println("<tbody>");
			for(int i = 0; i < contentInfo.floatFlagNum(); i++){
				out.println("<tr>");
				out.println("<td>");
				out.println(contentInfo.floatFlagName(i));
				out.println("</td>");
				out.println("<td>");
				out.println("<input type=\"number\" min=\"" + Float.MIN_VALUE + "\" max=\""
					+ Float.MAX_VALUE + "\" name=\"" + contentInfo.floatFlagName(i) + "\">");
				out.println("</td>");
				out.println("</tr>");
			}
			out.println("</tbody>");
			out.println("</table>");
		}

		out.println("<input type=\"submit\" value=\"Index\">");
		out.println("<input type=\"reset\" value=\"Reset\">");

		out.println("</form>");
		out.println("</body>");
		out.println("</html>");
	}
}
